﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business
{
    public enum SystemModule
    {
        CCD, //Customer Care Domestic
        CCO, //Customer Care Oversea
        CID, //Customer Info Domestic
        CIO, //Customer Info Oversea
        ACR, //ACR SAP (ACIS)
        EZVisit,
        Admin, //Administrator
    }

    public static class PermisionUtil
    {
        /// <summary>
        /// 根据CIL帐户，获取其对应的权限，返回一个数组
        /// </summary>
        public static string[] GetRoles(string cil, SystemModule module)
        {
            string belongsTo = module.ToString();
            using (var dc = new CCEntities())
            {
                return dc.Employees.First(p => p.NameEN == cil).
                    Roles.Where(p => p.BelongsTo == belongsTo).
                    Select(p => p.Name).ToArray();
            }
        }

        /// <summary>
        /// 根据CIL帐户，获取其对应的权限，返回一个适合SQL使用的字符串
        /// </summary>
        public static string GetRolesForSQL(string cil, SystemModule module)
        {
            var roles = GetRoles(cil, module);
            if (roles.Length == 0)
                return String.Empty;

            StringBuilder sb = new StringBuilder();
            foreach (var role in roles)
            {
                sb.AppendFormat("'{0}',", role);
            }
            //去掉最后一个,号
            return sb.Remove(0, sb.Length - 1).ToString();
        }
    }
}
